package com.tencent.liteav.demo.play.danke;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.tencent.liteav.demo.play.SuperPlayerConst;
import com.tencent.liteav.demo.play.SuperPlayerModel;
import com.tencent.liteav.demo.play.SuperPlayerView;

import androidx.annotation.NonNull;

/**
 * @author wpq
 * @version 1.0
 */
public class DKVideoPlayer extends FrameLayout implements IDKVideoPlayer {

    private SuperPlayerView mPlayerView;
    private boolean isAlreadyFullScreen;
    private int[] contentViewPadding;

    public DKVideoPlayer(Context context) {
        super(context);
        init();
    }

    public DKVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DKVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public DKVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mPlayerView = new SuperPlayerView(getContext());
        mPlayerView.setBackgroundColor(Color.BLACK);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(mPlayerView, params);
        addPlayerViewCallback(mPlayerCallback);
        if (getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();
            isAlreadyFullScreen = (activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                    == WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    private SuperPlayerView.PlayerViewCallback mPlayerCallback = new SuperPlayerView.PlayerViewCallback() {
        @Override
        public void hideViews() {
            if (mPlayerView == null || mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FULLSCREEN) {
                return;
            }
            Activity activity = null;
            if (getContext() instanceof Activity) {
                activity = (Activity) getContext();
            }
            DKVideoPlayerUtils.hideActionBar(getContext());
            if (activity != null) {
                if (!isAlreadyFullScreen) {
                    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                }
                ViewGroup contentView = activity.findViewById(android.R.id.content);
                if (mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FLOAT) {
                    contentView.removeView(mPlayerView);
                } else {
                    removeView(mPlayerView);
                }
                if (contentViewPadding == null) {
                    contentViewPadding = new int[4];
                }
                contentViewPadding[0] = contentView.getPaddingLeft();
                contentViewPadding[1] = contentView.getPaddingTop();
                contentViewPadding[2] = contentView.getPaddingRight();
                contentViewPadding[3] = contentView.getPaddingBottom();
                contentView.setPadding(0, 0, 0, 0); // 全屏
                LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                contentView.addView(mPlayerView, params);
            }
        }

        @Override
        public void showViews() {
            if (mPlayerView != null && mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FULLSCREEN) {
                Activity activity = null;
                if (getContext() instanceof Activity) {
                    activity = (Activity) getContext();
                }
                DKVideoPlayerUtils.showActionBar(getContext());
                if (activity != null) {
                    if (!isAlreadyFullScreen) {
                        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    }
                    ViewGroup contentView = activity.findViewById(android.R.id.content);
                    contentView.removeView(mPlayerView);
                    if (contentViewPadding != null) {
                        // 恢复
                        contentView.setPadding(contentViewPadding[0], contentViewPadding[1], contentViewPadding[2], contentViewPadding[3]);
                        contentViewPadding = null;
                    }
                    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                    addView(mPlayerView, params);
                }
            }
        }

        @Override
        public void onQuit(int playMode) {

        }
    };

    @Override
    public void hideSmallTopLayout() {
        if (mPlayerView != null) {
            mPlayerView.hideSmallTopLayout();
        }
    }

    @Override
    public void addPlayerViewCallback(SuperPlayerView.PlayerViewCallback callback) {
        if (mPlayerView != null) {
            mPlayerView.addPlayerViewCallback(callback);
        }
    }

    @Override
    public void playWithMode(SuperPlayerModel superPlayerModel) {
        DKVideoPlayerManager.getInstance().setCurrentPlayer(this);
        if (mPlayerView != null) {
            mPlayerView.playWithMode(superPlayerModel);
        }
    }

    @Override
    public void onPause() {
        if (mPlayerView != null && mPlayerView.getPlayMode() != SuperPlayerConst.PLAYMODE_FLOAT) {
            mPlayerView.onPause();
        }
    }

    @Override
    public void onResume() {
        if (mPlayerView != null && mPlayerView.getPlayState() == SuperPlayerConst.PLAYSTATE_PLAY) {
            DKVideoPlayerManager.getInstance().setCurrentPlayer(this);
            mPlayerView.onResume();
            if (mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FLOAT) {
                mPlayerView.requestPlayMode(SuperPlayerConst.PLAYMODE_WINDOW);
            }
        }

        // 恢复全屏
//            if (mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FULLSCREEN) {
//                //隐藏虚拟按键，并且全屏
//                View decorView = getWindow().getDecorView();
//                if (decorView != null) {
//                    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
//                    decorView.setSystemUiVisibility(uiOptions);
//                }
//            }
    }

    @Override
    public void release() {
        try {
            // 释放
            mPlayerView.release();
            if (mPlayerView.getPlayMode() != SuperPlayerConst.PLAYMODE_FLOAT) {
                mPlayerView.resetPlayer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mPlayerView = null;
        }
    }

    @Override
    public boolean onBackPressed() {
        // 如果是全屏
        if (mPlayerView != null && mPlayerView.getPlayMode() == SuperPlayerConst.PLAYMODE_FULLSCREEN) {
            // 恢复竖屏
            mPlayerView.requestPlayMode(SuperPlayerConst.PLAYMODE_WINDOW);
            return true;
        }
        return false;
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == View.VISIBLE) {
            onResume();
        } else {
            onPause();
        }
    }
}
